# -*- coding: utf-8 -*-
#  @Time    : 2020/2/4 11:05
#  @Author  : Ben
#  @File    : service.py
from user.models import User


class UserService:

    def register_service(self, user_name, password, question, answer):
        """
        用户注册Service
        :param answer:
        :param question:
        :param password:
        :param user_name:
        :return: boolean
        """
        return_user = User.user.get_or_create(userName=user_name, password=password, question=question,
                                              answer=answer)
        if return_user is True:
            print("注册成功")
            return True
        else:
            print("注册失败")
            return False

    def login_service(self, user_name, password):
        """
         用户登录Service
        :param user_name:
        :param password:
        :return: boolean
        """
        try:
            return_user = User.user.get(userName=user_name)
            print(return_user)
            if return_user.password == password:
                print("账号密码正确，登录成功")

                return True
            else:
                print("账号密码错误，登录失败")
                return False
        except Exception:
            print("查无次用户，登录失败，请检查用户名和密码")
            return False